package com.xing.hosp.controller.api;

import com.alibaba.excel.util.StringUtils;
import com.xing.common.exception.HospitalException;
import com.xing.common.helper.HttpRequestHelper;
import com.xing.common.result.Result;
import com.xing.common.result.ResultCodeEnum;
import com.xing.common.utils.YyghException;
import com.xing.hosp.service.DepartmentService;
import com.xing.hosp.service.HospitalService;
import com.xing.hosp.service.HospitalSetService;
import com.xing.hosp.service.ScheduleService;
import com.xing.hosp.vo.hosp.ScheduleQueryVo;
import com.xing.hospital.common.utils.MD5;
import com.xing.hospital.model.hosp.Department;
import com.xing.hospital.model.hosp.DepartmentQueryVo;
import com.xing.hospital.model.hosp.Hospital;
import com.xing.hospital.model.hosp.Schedule;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author: xingfuhao
 * @Date: 2022/05/27/17:13
 */

@RestController
@RequestMapping("/api/hosp")
//@CrossOrigin
@Slf4j
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;


    @ApiOperation(value = "上传医院接口")
    @PostMapping("/saveHospital")
    //通过HttpServletRequest来获取传来的数据
    public Result saveHospital(HttpServletRequest request) {

        //获取传递过来的医院信息
        //将Map<String,String[]>转化为Map<String,Object>
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(requestMap);

        //1.获取医院系统传递过来的签名，签名进行MD5加密
        String hospSign = (String) parameterMap.get("sign");
        log.info("医院传递过来的签名：" + hospSign);


        //2.获取传递过来的医院编码，查询数据库，查询签名
        String hoscode = (String) parameterMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            throw new YyghException(com.xing.common.utils.ResultCodeEnum.PAEAM_ERROR);
        }
        String signKey = hospitalSetService.getSignKey(hoscode);
        log.info("数据库查询出来的签名：" + signKey);


        //3.把数据库查询的签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);
        log.info("MD5加密后的签名：" + signKeyMd5);

        //传输过程中“+”转换为了“ ”，因此我们要转换回来了
        String logoData = (String) parameterMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        parameterMap.put("logoData", logoData);


//        4.判断签名是否一致
//        if(!HttpRequestHelper.isSignEquals(parameterMap,hospitalSetService.getSignKey(hoscode))){
//            throw new YyghException(com.xing.common.utils.ResultCodeEnum.SIGN_ERROR);
//        }


        //保存医院信息到mongoDB中去
        hospitalService.save(parameterMap);
        return Result.ok();
    }


    @ApiOperation("查询医院")
    @PostMapping("/hospital/show")
    public Result getHospital(HttpServletRequest request) {

        //获取传递过来医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //1 获取医院系统传递过来的签名,签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");

        //2 根据传递过来医院编码，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);

        //3 把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //4 判断签名是否一致
        if (hospSign.equals(signKeyMd5)) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service方法实现根据医院编号查询
        Hospital hospital = hospitalService.getByHascode(hoscode);

        return Result.ok(hospital);
    }

    @ApiOperation("上传科室接口")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        //获取传递过来的科室信息
        Map<String, String[]> requestParameterMap = request.getParameterMap();
        //封装的方法，转化map
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestParameterMap);

        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");

        //1.获取医院系统传递过来的签名，签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");

        //2.根据传递过来医院编码，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);

        //3.把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //4.判断签名是否一致
        if (hospSign.equals(signKeyMd5)) {
            throw new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service方法
        departmentService.save(paramMap);
        return Result.ok();
    }


    @ApiOperation(value = "查询科室接口，获取分页列表")
    @PostMapping("/department/list")
    public Result findDepartment(HttpServletRequest request) {
        //获取传递过来的科室信息
        //获取传递过来的科室信息
        Map<String, String[]> requestParameterMap = request.getParameterMap();
        //封装的方法，转化map
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestParameterMap);

        //医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //当前页和每页记录数
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));
        //TODO 去做 签名校验

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        //调用service方法
        Page<Department> pageModel = departmentService.findPageDepartment(page, limit, departmentQueryVo);
        return Result.ok(pageModel);
    }

    //删除科室接口
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        //获取传递过来的科室信息
        Map<String, String[]> requestParameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestParameterMap);

        //医院编号和科室编号
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");

        //TODO 签名校验是一样的
        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }

    //上传排版接口
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        //获取传递过来的科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //TODO 意思还没做的 签名校验
        scheduleService.save(paramMap);
        return Result.ok();
    }

    //查询排班接口
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request) {
        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //医院编号
        String hoscode = (String) paramMap.get("hoscode");

        //科室编号
        String depcode = (String) paramMap.get("depcode");

        //当前页和每页记录数
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));
        //TODO 签名校验

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);

        //调用service方法
        Page<Schedule> pageModel = scheduleService.findPageSchedule(page, limit, scheduleQueryVo);
        return Result.ok(pageModel);
    }

    //删除排班
    @PostMapping("schedule/remove")
    public Result remove(HttpServletRequest request) {
        //获取传递过来的科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院编号和排班编号
        String hoscode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");

        //TODO 签名校验
        scheduleService.remove(hoscode, hosScheduleId);

        return Result.ok();


    }


}
